package com.jianbo.proxy.core;

import java.io.PrintStream;

import org.smartboot.http.common.logging.Logger;
import org.smartboot.http.common.logging.LoggerFactory;
import org.smartboot.http.server.HttpRequest;
import org.smartboot.http.server.HttpResponse;

import com.jianbo.proxy.util.ByteUtil;

public class ErrorGlobalHandler {

    private static Logger logger = LoggerFactory.getLogger(ErrorGlobalHandler.class);

    public static void response(RuntimeException error, HttpRequest request, HttpResponse response) {
        try {
            response.getOutputStream().write(
                ByteUtil.bytes("<div>" + error.getMessage() + "</div>")
            );

            response.getOutputStream().write(ByteUtil.bytes("<pre style='width:1000%'>"));
            error.printStackTrace(new PrintStream(response.getOutputStream()));
            response.getOutputStream().write(ByteUtil.bytes("</pre>"));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }
    
}
